home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WWTCLKit / WWProcView.m < prev    next >
Encoding:
Text File  |  1995-03-22  |  14.8 KB  |  559 lines

  1.  
  2. #import "WWTCLInterp.h"
  3. #import "WWVarView.h"
  4. #import <ctype.h>
  5.  
  6. @implementation WWVarView
  7.  
  8. // the idea here is that this view has a tcl variable name associated
  9. // with it.  When a corresponding WWVarValueView is dragged over it,
  10. // it accepts the drag, sets its image to the contents of the
  11. // WWVarValueView and sets the value of it's tcl variable to the
  12.  
  13. + initialize { [WWVarView setVersion:3]; return self; }
  14.  
  15.  
  16. - initFrame:(const NXRect *)frameRect
  17. {
  18.    const char  *dragTypes[] = { NXAsciiPboardType, NULL};
  19.  
  20.  
  21.    [super initFrame:frameRect];
  22.    [self registerForDraggedTypes:dragTypes count:1];
  23.  
  24.    tclVarNameSize = 32;
  25.    tclVarName = (char *)NXZoneCalloc([self zone], tclVarNameSize, sizeof(char));
  26.    tclVarValueSize = 32;
  27.    tclVarValue = (char *)NXZoneCalloc([self zone], tclVarValueSize, sizeof(char));
  28.    tclVarTypeSize = 32;
  29.    tclVarType = (char *)NXZoneCalloc([self zone], tclVarTypeSize, sizeof(char));
  30.    // <null pointer> is around 15 characters, so add (say) 40 bytes to cmdBuf...
  31.    cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclVarNameSize + tclVarValueSize), sizeof(char));
  32.    framed = NO;
  33.    restrictDropToSameName = NO;
  34.    restrictDropToSameType = NO;
  35.    actAsSource = YES;
  36.    actAsSink = YES;
  37.    frameColor = NX_COLORBLACK;
  38.    frameWidth = 3.0;
  39.    nameSwitchView = nil;
  40.    valueSwitchView = nil;
  41.    typeSwitchView = nil;
  42.  
  43.    return self;
  44. }
  45.  
  46. - awake
  47. {
  48.    const char  *dragTypes[] = { NXAsciiPboardType, NULL};
  49.  
  50.  
  51.    [super awake];
  52.    [self registerForDraggedTypes:dragTypes count:1];
  53.  
  54.    framed = NO;
  55.    cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclVarNameSize + tclVarValueSize), sizeof(char));
  56.    
  57.    return self;
  58. }
  59.  
  60. - free
  61. {
  62.    const char *fileType[] = { "" };
  63.    Pasteboard *pboard = [Pasteboard newName:NXDragPboard];
  64.  
  65.  
  66.    [pboard declareTypes:fileType num:0 owner:nil];
  67.    [self unregisterDraggedTypes];
  68.    image = nil; // don't free the potentially shared image
  69.    if (tclVarName) { free(tclVarName); }
  70.    if (tclVarValue) { free(tclVarValue); }
  71.    if (tclVarType) { free(tclVarType); }
  72.    if (cmdBuf) { NXZoneFree([self zone], cmdBuf); }
  73.  
  74.    return [super free];
  75. }
  76.  
  77. - reinitializeStringsFromZone:(NXZone *)zone
  78. {
  79.   tclVarName = (char *)NXZoneCalloc(zone, tclVarNameSize, sizeof(char));
  80.   tclVarValue = (char *)NXZoneCalloc(zone, tclVarValueSize, sizeof(char));
  81.   tclVarType = (char *)NXZoneCalloc(zone, tclVarTypeSize, sizeof(char));
  82.   cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclVarNameSize + tclVarValueSize), sizeof(char));
  83.   return self;
  84. }
  85.  
  86. - copyFromZone:(NXZone *)zone
  87. {
  88.   id newCopy = [super copyFromZone:zone];
  89.  
  90.   [newCopy reinitializeStringsFromZone:zone];
  91.   [newCopy setTclVarName:tclVarName];
  92.   [newCopy setTclVarValue:tclVarValue];
  93.   [newCopy setTclVarType:tclVarType];
  94.  
  95.   return newCopy;
  96. }
  97.  
  98. - drawSelf:(const NXRect *)rects :(int)num
  99. {
  100.   [super drawSelf:rects :num];
  101.   if (framed)
  102.   {  NXSetColor(frameColor);
  103.      NXFrameRectWithWidth(&bounds, (NXCoord)frameWidth);
  104.   }
  105.   return self;
  106. }
  107.  
  108. /*-------------------- methods to be the source of a dragging operation */
  109.  
  110. - mouseDown:(NXEvent *)theEvent
  111. {
  112.    NXPoint  zero = {0.0, 0.0};
  113.    id       thePboard;
  114.    const char  *dragTypes[] = { NXAsciiPboardType, NULL};
  115.    static char *fooBarBaz = "fooBarBaz";
  116.  
  117.     
  118.    if (!image) { return self; }
  119.    if (!actAsSource) { return self; }
  120.  
  121.    // Prevent default window ordering (this is a new appkit feature)
  122.    [NXApp preventWindowOrdering];
  123.  
  124.    //get the Pboard
  125.    thePboard = [Pasteboard newName:NXDragPboard];
  126.    [thePboard declareTypes:dragTypes num:1 owner:nil];
  127.  
  128.    // we're not really going to use this info, we're just sticking something on the board...
  129.    [thePboard writeType:NXAsciiPboardType data:fooBarBaz length:(1 + strlen(fooBarBaz))];
  130.     
  131.    //start the drag
  132.    [self dragImage:image    // visible on screen during drag
  133.                 at:&zero    // offset the mouse point for the drag
  134.             offset:&zero    // offset the inital mouse pt
  135.              event:theEvent    // theEvent structure
  136.         pasteboard:thePboard    // a Pasteboard with data on it
  137.             source:self        // source object
  138.          slideBack:YES];    // if no destination animate back to source
  139.     
  140.     return self;
  141. }
  142.  
  143. - draggedImage:(NXImage *)image beganAt:(NXPoint *)screenPoint  { return self; }
  144.  
  145. - (NXDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag  { return (NX_DragOperationCopy | NX_DragOperationGeneric); }
  146.  
  147. - draggedImage:(NXImage *)image endedAt:(NXPoint *)screenPoint deposited:(BOOL)flag { return self; }
  148.  
  149. /*-------------------- methods to be the destination of a dragging operation */
  150.  
  151. - (NXDragOperation)draggingEntered:sender
  152. {
  153.    if (!actAsSink)
  154.    {  if (framed)
  155.       {  framed = NO;
  156.      [self display];
  157.       }
  158.       return NX_DragOperationNone;
  159.    }
  160.  
  161.    // check to make sure we are not the source and this operation is a copy
  162.    if (   ([sender draggingSourceOperationMask] & NX_DragOperationCopy) 
  163.        && ([sender draggingSource] != self)
  164.        && ([[sender draggingSource] respondsTo:@selector(tclVarValue)]))
  165.    {  
  166.       // if we're selective, need to check the var's name
  167.       // if it's not the same as ours, we're outta here
  168.       if (restrictDropToSameName)
  169.       {  if (tclVarName && [[sender draggingSource] tclVarName] && strcmp(tclVarName, [[sender draggingSource] tclVarName]))
  170.          {  if (framed)
  171.         {  framed = NO;
  172.            [self display];
  173.          }
  174.             return NX_DragOperationNone;
  175.      }
  176.       }
  177.       if (restrictDropToSameType)
  178.       {  if (tclVarType && [[sender draggingSource] tclVarType] && strcmp(tclVarType, [[sender draggingSource] tclVarType]))
  179.          {  if (framed)
  180.         {  framed = NO;
  181.            [self display];
  182.          }
  183.             return NX_DragOperationNone;
  184.      }
  185.       }
  186.       framed = YES;
  187.       [self display];
  188.       //return the type of operation we want to do, this will dictate
  189.       //the type of cursor will show up when accepting the drag
  190.       return NX_DragOperationCopy;
  191.    }
  192.    if (framed) 
  193.    {  framed = NO;
  194.       [self display];
  195.    }
  196.    return NX_DragOperationNone;
  197. }
  198.  
  199. - (NXDragOperation)draggingUpdated:sender
  200. {
  201.    if (!actAsSink)
  202.    {  if (framed)
  203.       {  framed = NO;
  204.      [self display];
  205.       }
  206.       return NX_DragOperationNone;
  207.    }
  208.  
  209.    // make sure that this is a copy operation, and that this instance
  210.    // of DragView is not the source
  211.    if (   ([sender draggingSourceOperationMask] & NX_DragOperationCopy) 
  212.        && ([sender draggingSource] != self)
  213.        && ([[sender draggingSource] respondsTo:@selector(tclVarValue)]))
  214.     { 
  215.       if (restrictDropToSameName)
  216.       {  if (tclVarName && [[sender draggingSource] tclVarName] && strcmp(tclVarName, [[sender draggingSource] tclVarName]))
  217.          {  if (framed)
  218.         {  framed = NO;
  219.            [self display];
  220.          }
  221.             return NX_DragOperationNone;
  222.      }
  223.       }
  224.       if (restrictDropToSameType)
  225.       {  if (tclVarType && [[sender draggingSource] tclVarType] && strcmp(tclVarType, [[sender draggingSource] tclVarType]))
  226.          {  if (framed)
  227.         {  framed = NO;
  228.            [self display];
  229.          }
  230.             return NX_DragOperationNone;
  231.      }
  232.       }
  233.       framed = YES;
  234.       [self display];
  235.       return NX_DragOperationCopy;
  236.    }
  237.    if (framed) 
  238.    {  framed = NO;
  239.       [self display];
  240.    }
  241.    return NX_DragOperationNone;
  242. }
  243.  
  244. - draggingExited:sender
  245. {
  246.    if (!actAsSink)
  247.    {  if (framed)
  248.       {  framed = NO;
  249.      [self display];
  250.       }
  251.       return NX_DragOperationNone;
  252.    }
  253.  
  254.    if (framed) 
  255.    {  framed = NO;
  256.       [self display];
  257.    }
  258.    return self;
  259. }
  260.  
  261. - (BOOL)prepareForDragOperation:sender  
  262. {  
  263.    if (!actAsSink)
  264.    {  if (framed)
  265.       {  framed = NO;
  266.      [self display];
  267.       }
  268.       return NO;
  269.    }
  270.    return YES; 
  271. }
  272.  
  273. static int notJustBlanks(const char *str)
  274. {
  275.   const char  *ptr;
  276.  
  277.  
  278.   ptr = str;
  279.   if (ptr)
  280.   {  while (*ptr && isspace(*ptr))
  281.      {  ptr++;
  282.      }
  283.      if (*ptr) // if we're not at the end and we stopped, it's not just blanks...
  284.      {  return 1;
  285.      }
  286.   }
  287.   return 0;
  288. }
  289.  
  290.  
  291. - (BOOL)performDragOperation:sender
  292. {
  293.    if (!actAsSink)
  294.    {  if (framed)
  295.       {  framed = NO;
  296.      [self display];
  297.       }
  298.       return NO;
  299.    }
  300.  
  301.    // make sure that what we are getting is from the same application
  302.    // and did not originate from this view.
  303.    if (   [sender isDraggingSourceLocal] 
  304.        && ([sender draggingSource] != self) 
  305.        && ([[sender draggingSource] respondsTo:@selector(tclVarValue)]))
  306.  
  307.    {  // should check to make sure the sender will respond appropriately...
  308.       if (restrictDropToSameName)
  309.       {  if (tclVarName && [[sender draggingSource] tclVarName] && strcmp(tclVarName, [[sender draggingSource] tclVarName]))
  310.          {  if (framed) 
  311.         {  framed = NO;
  312.            [self display];
  313.         }
  314.         return NO;
  315.      }
  316.       }
  317.       if (restrictDropToSameType)
  318.       {  if (tclVarType && [[sender draggingSource] tclVarType] && strcmp(tclVarType, [[sender draggingSource] tclVarType]))
  319.          {  if (framed) 
  320.             {  framed = NO;
  321.            [self display];
  322.         }
  323.         return NO;
  324.      }
  325.       }
  326.       [self setImage:nil];
  327.       [self setImage:[[sender draggingSource] image]];
  328.       [self setTclVarValue:[[sender draggingSource] tclVarValue]];
  329.       if (nameSwitchView)
  330.       {  //[nameSwitchView showViewNamed:tclVarName];
  331.       }
  332.       if (typeSwitchView)
  333.       {  //[typeSwitchView showViewNamed:tclVarType];
  334.       }
  335.       if (notJustBlanks(tclVarValue))
  336.       {  // cmdBuf is basically "set <tclVarName> <tclVarValue>"
  337.          if (valueSwitchView)
  338.          {  //[valueSwitchView showViewNamed:tclVarValue];
  339.          }
  340.          sprintf(cmdBuf, "set %s %s", tclVarName, tclVarValue);
  341.          [interp globalEval:cmdBuf];
  342.       }
  343.       framed = NO;
  344.       [self display];
  345.    }
  346.    if (framed) 
  347.    {  framed = NO;
  348.       [self display];
  349.    }
  350.    return YES;
  351. }
  352.  
  353. - concludeDragOperation:sender {  return self; }
  354.  
  355. /*-------------------- methods to display, and other useful stuff */
  356.  
  357. - (BOOL)acceptsFirstMouse  {  return YES; }
  358.  
  359. - (BOOL)shouldDelayWindowOrderingForEvent:(NXEvent *)theEvent {  return YES; }
  360.  
  361. // set and get stuff
  362.  
  363. - setTclVarValue:(const char *)str 
  364.   int   cnt;
  365.  
  366.  
  367.   if (tclVarValueSize <= 0) 
  368.   {  tclVarValueSize = 32; 
  369.      tclVarValue = (char *)NXZoneCalloc([self zone], tclVarValueSize, sizeof(char));
  370.      cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclVarNameSize + tclVarValueSize), sizeof(char));
  371.   }
  372.   cnt = strlen(str);
  373.   while (cnt >= tclVarValueSize)
  374.   {  tclVarValueSize *= 2;
  375.      tclVarValue = (char *)NXZoneRealloc([self zone], tclVarValue, tclVarValueSize);
  376.   }
  377.   cmdBuf = (char *)NXZoneRealloc([self zone], cmdBuf, (40 + tclVarNameSize + tclVarValueSize));
  378.   strcpy(tclVarValue, str); 
  379.   return self; 
  380. }
  381. //
  382. - (const char *)tclVarValue { return tclVarValue; }
  383.  
  384.  
  385. - setTclVarName:(const char *)str 
  386.   int   cnt;
  387.  
  388.  
  389.   if (tclVarNameSize <= 0) 
  390.   {  tclVarNameSize = 32; 
  391.      tclVarName = (char *)NXZoneCalloc([self zone], tclVarNameSize, sizeof(char));
  392.      cmdBuf = (char *)NXZoneCalloc([self zone], (40 + tclVarNameSize + tclVarValueSize), sizeof(char));
  393.   }
  394.   cnt = strlen(str);
  395.   while (cnt >= tclVarNameSize)
  396.   {  tclVarNameSize *= 2;
  397.      tclVarName = (char *)NXZoneRealloc([self zone], tclVarName, tclVarNameSize);
  398.   }
  399.   cmdBuf = (char *)NXZoneRealloc([self zone], cmdBuf, (40 + tclVarNameSize + tclVarValueSize));
  400.   strcpy(tclVarName, str); 
  401.   return self; 
  402. }
  403. //
  404. - (const char *)tclVarName { return tclVarName; }
  405.  
  406. - setTclVarType:(const char *)str 
  407.   int   cnt;
  408.  
  409.  
  410.   if (tclVarTypeSize <= 0) 
  411.   {  tclVarTypeSize = 32; 
  412.      tclVarType = (char *)NXZoneCalloc([self zone], tclVarTypeSize, sizeof(char));
  413.   }
  414.   cnt = strlen(str);
  415.   while (cnt >= tclVarTypeSize)
  416.   {  tclVarTypeSize *= 2;
  417.      tclVarType = (char *)NXZoneRealloc([self zone], tclVarType, tclVarTypeSize);
  418.   }
  419.   strcpy(tclVarType, str); 
  420.   return self; 
  421. }
  422. //
  423. - (const char *)tclVarType { return tclVarType; }
  424.  
  425.  
  426. - (BOOL)restrictDropToSameName { return restrictDropToSameName; }
  427. - setRestrictDropToSameName:(BOOL)flag {  restrictDropToSameName = flag; return self; }
  428.  
  429. - (BOOL)restrictDropToSameType { return restrictDropToSameType; }
  430. - setRestrictDropToSameType:(BOOL)flag {  restrictDropToSameType = flag; return self; }
  431.  
  432. - (BOOL)actAsSink { return actAsSink; }
  433. - setActAsSink:(BOOL)flag 
  434. {  if (actAsSink && !flag) // we're about to turn it off...
  435.    {  [self unregisterDraggedTypes];
  436.    }
  437.    if (!actAsSink && flag) // we're about to turn it on...
  438.    {  const char  *dragTypes[] = { NXAsciiPboardType, NULL};
  439.       [self registerForDraggedTypes:dragTypes count:1];
  440.    }
  441.    
  442.    actAsSink = flag; 
  443.    return self; 
  444. }
  445.  
  446. - (BOOL)actAsSource { return actAsSource; }
  447. - setActAsSource:(BOOL)flag {  actAsSource = flag; return self; }
  448.  
  449. // IB stuff
  450. - (const char *)getInspectorClassName 
  451. {  
  452.    NXEvent  *event = [NXApp currentEvent];
  453.  
  454.    if (event->flags & NX_ALTERNATEMASK)
  455.    {  return [super getInspectorClassName];
  456.    }
  457.    
  458.    return "WWVarViewIBInspector"; 
  459. }
  460.  
  461. - write:(NXTypedStream *)stream 
  462. {
  463.    [super write:stream];
  464.    NXWriteTypes(stream, "***cccfc", &tclVarName, &tclVarValue, &tclVarType, 
  465.         &restrictDropToSameName, &restrictDropToSameType, &actAsSource, &frameWidth, &actAsSink);
  466.    NXWriteColor(stream, frameColor);
  467.    return self;
  468. }
  469. //
  470. - read:(NXTypedStream *)stream 
  471. {
  472.    int version;
  473.  
  474.    [super read:stream];
  475.  
  476.    version = NXTypedStreamClassVersion(stream, "WWVarView");
  477.    if (version == 1)
  478.    {  NXReadTypes(stream, "***ccc", 
  479.           &tclVarName, &tclVarValue, &tclVarType, 
  480.           &restrictDropToSameName, &restrictDropToSameType, &actAsSource);
  481.       frameColor = NX_COLORBLACK;
  482.       if (tclVarName)
  483.       {  tclVarNameSize = strlen(tclVarName) + 1;
  484.       }
  485.       else
  486.       {  tclVarNameSize = 0;
  487.       }
  488.       if (tclVarValue)
  489.       {  tclVarValueSize = strlen(tclVarValue) + 1;
  490.       }
  491.       else
  492.       {  tclVarValueSize = 0;
  493.       }
  494.       if (tclVarType)
  495.       {  tclVarTypeSize = strlen(tclVarType) + 1;
  496.       }
  497.       else
  498.       {  tclVarTypeSize = 0;
  499.       }
  500.    }
  501.    if (version == 2)
  502.    {  NXReadTypes(stream, "***cccf", 
  503.           &tclVarName, &tclVarValue, &tclVarType, 
  504.           &restrictDropToSameName, &restrictDropToSameType, &actAsSource, &frameWidth);
  505.       frameColor = NXReadColor(stream);
  506.  
  507.       if (tclVarName)
  508.       {  tclVarNameSize = strlen(tclVarName) + 1;
  509.       }
  510.       else
  511.       {  tclVarNameSize = 0;
  512.       }
  513.       if (tclVarValue)
  514.       {  tclVarValueSize = strlen(tclVarValue) + 1;
  515.       }
  516.       else
  517.       {  tclVarValueSize = 0;
  518.       }
  519.       if (tclVarType)
  520.       {  tclVarTypeSize = strlen(tclVarType) + 1;
  521.       }
  522.       else
  523.       {  tclVarTypeSize = 0;
  524.       }
  525.    }
  526.    if (version == 3)
  527.    {  NXReadTypes(stream, "***cccfc", 
  528.           &tclVarName, &tclVarValue, &tclVarType, 
  529.           &restrictDropToSameName, &restrictDropToSameType, &actAsSource, &frameWidth, &actAsSink);
  530.       frameColor = NXReadColor(stream);
  531.  
  532.       if (tclVarName)
  533.       {  tclVarNameSize = strlen(tclVarName) + 1;
  534.       }
  535.       else
  536.       {  tclVarNameSize = 0;
  537.       }
  538.       if (tclVarValue)
  539.       {  tclVarValueSize = strlen(tclVarValue) + 1;
  540.       }
  541.       else
  542.       {  tclVarValueSize = 0;
  543.       }
  544.       if (tclVarType)
  545.       {  tclVarTypeSize = strlen(tclVarType) + 1;
  546.       }
  547.       else
  548.       {  tclVarTypeSize = 0;
  549.       }
  550.    }
  551.    return self;
  552. }
  553.  
  554.  
  555. @end
  556.